package api

import (
	"ginfast/app/{{.App}}/dto"
	"ginfast/app/{{.App}}/model"
	"ginfast/pkg/orm"
	"ginfast/pkg/ginx"
	"ginfast/util"
)

// {{.Model}}
type {{.Model}} struct {
}

// ListHandler 列表
func ({{.Model}}) ListHandler(c *ginx.Context) {
	var p dto.Where
	if err := c.MustBind( &p); err != nil {
		return
	}
	var data []model.{{.Model}}
	total, _ := p.DbModelWhere(&model.{{.Model}}{}).Find(&data)
	c.JSONWriteTotal(total, data)
}

// GetHandler 获取指定id
func ({{.Model}}) GetHandler(c *ginx.Context) {
	util.QueryByID(&model.{{.Model}}{}, c)
}

// AddHandler 新增
func ({{.Model}}) AddHandler(c *ginx.Context) {
	var p model.{{.Model}}
	//获取参数
	err := c.MustBind( &p.{{.Model}}Opt)
	if err != nil {
		return
	}
	err = orm.DbCreate(&p);
	c.JSONWriteError(err)
}

// UpdateHandler 修改
func ({{.Model}}) UpdateHandler(c *ginx.Context) {
	var p model.{{.Model}}
	//获取参数
	err := c.MustBind( &p.{{.Model}}Opt)
	if err != nil {
		return
	}
	err = orm.DbUpdateModel(&p)
	c.JSONWriteError(err)
}

// DeleteHandler 删除
func ({{.Model}}) DeleteHandler(c *ginx.Context) {
	util.Deletes(&model.{{.Model}}{}, c)
}

func(o {{.Model}}) Routers(r *ginx.RouterGroup) {
	r.GET("/list", o.ListHandler)
	r.GET("/:id", o.GetHandler)
	r.POST("", o.AddHandler)
	r.PUT("", o.UpdateHandler)
	r.DELETE("/:id", o.DeleteHandler)
}